﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;
using GISDataAdapter.Data.Object.ESRI;
using DAL;

namespace GISDataAdapter.Data
{
    public class ShpFileLoader
    {
        /// <summary>
        /// 读取SHP文件
        /// </summary>
        /// <param name="fileNameList">SHP文件集合</param>
        /// <returns></returns>
        public List<DataTable> Load(List<string> fileNameList)
        {
            List<DataTable> resultCollection = new List<DataTable>();

            foreach (string fileName in fileNameList)
            {
                byte[] shp = File.ReadAllBytes(fileName);
                DataTable result = new DataTable();
                result.Columns.Add("RowID");
                result.Columns.Add("Longitude");
                result.Columns.Add("Latitude");

                //这里是要素类型：点、线、面
                Geometry geoType = (Geometry)BitConverter.ToUInt32(shp, 32);

                switch (geoType)
                {
                    case Geometry.Point:
                        {
                            //从第100字节后每隔28字节为一条记录
                            for (int i = 100; i < shp.Length; i = i + 28)
                            {
                                byte[] byteTrans = new byte[4];
                                byteTrans[0] = shp[i + 3];
                                byteTrans[1] = shp[i + 2];
                                byteTrans[2] = shp[i + 1];
                                byteTrans[3] = shp[i];

                                int recordId = BitConverter.ToInt32(byteTrans, 0);
                                double x = BitConverter.ToDouble(shp, i + 12);
                                double y = BitConverter.ToDouble(shp, i + 20);
                                DataRow row = result.NewRow();
                                row.ItemArray = new object[] { recordId, x, y };
                                result.Rows.Add(row);
                            }

                            resultCollection.Add(result);

                            break;
                        }
                    case Geometry.PolyLine:
                        {
                            break;
                        }
                    case Geometry.Polygon:
                        {
                            break;
                        }
                    case Geometry.MultiLine:
                        {
                            break;
                        }
                    case Geometry.MultiPoint:
                        {
                            //MultiPoint类型数据每隔64位一条记录
                            for (int i = 100; i < shp.Length; i = i + 64)
                            {
                                byte[] byteTrans = new byte[4];
                                byteTrans[0] = shp[i + 3];
                                byteTrans[1] = shp[i + 2];
                                byteTrans[2] = shp[i + 1];
                                byteTrans[3] = shp[i];

                                int recordId = BitConverter.ToInt32(byteTrans, 0);
                                double x = BitConverter.ToDouble(shp, i + 12);
                                double y = BitConverter.ToDouble(shp, i + 20);
                                DataAccess.InsertData(recordId, x, y);
                                DataRow row = result.NewRow();
                                row.ItemArray = new object[] { recordId, x, y };
                                result.Rows.Add(row);
                            }

                            resultCollection.Add(result);

                            break;
                        }
                    default:
                        {
                            break;
                        }
                }
            }

            //SHP数据表集合，读取的列包括RowID, Longitude, Latitude
            return resultCollection;
        }
    }
}
